package Interface.User;

import Mapper.Admin.yuanGongMapper;
import Mapper.Admin.zhujiemian;
import Mapper.User.huodongMapper;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import pojo.huodongfabu;
import pojo.yuangong;

import java.io.FileInputStream;

public class jianShenHuoDong extends Application {
    Mapper.User.huodongMapper huodongMapper;
    private static final int ITEMS_PER_PAGE = 15;
    private TableView<huodongfabu> tableView;
    private Pagination pagination;

    private TextField t_id;
    private TextField t_jieshao;
    private TextField t_shetuanming;
    @Override
    public void start(Stage stage) throws Exception {
        FileInputStream input=new FileInputStream("C:\\Users\\86178\\Videos\\Captures\\走路.gif");
        Image image = new Image(input);
        ImageView imageView =new ImageView(image);
        imageView.setFitWidth(488);
        imageView.setFitHeight(465);

        tableView = createTableView();
        pagination = new Pagination();


        //实例化不然为null
        huodongMapper =new huodongMapper();

        /**
         * 获取数据
         */
        pagination.setPageFactory(pageIndex -> {
            ObservableList<huodongfabu> data = huodongMapper.fetchDataFromDatabase(pageIndex * ITEMS_PER_PAGE, ITEMS_PER_PAGE);
            tableView.setItems(data);
            return new VBox(tableView);
        });

        StackPane root = new StackPane();
        root.getChildren().add(pagination);
        stage.setScene(new Scene(root, 650, 650));
        stage.setTitle("健身活动");
//        stage.setWidth(650);
//        stage.setHeight(650);
        stage.setResizable(false);//窗口不可拉伸
        stage.show();
    }

    // 添加一个方法用于刷新数据
    private void refreshData() {
        int currentPageIndex = pagination.getCurrentPageIndex();
        pagination.setPageFactory(pageIndex -> {
            ObservableList<huodongfabu> data = huodongMapper.fetchDataFromDatabase(pageIndex * ITEMS_PER_PAGE, ITEMS_PER_PAGE);
            tableView.setItems(data);
            return new VBox(tableView);
        });
        // 设置回之前的页数
        pagination.setCurrentPageIndex(currentPageIndex);
    }


    private TableView<huodongfabu> createTableView() {
        TableView<huodongfabu> tableView = new TableView<>();

        TableColumn<huodongfabu, String> id = new TableColumn<>("名字");
        id.setCellValueFactory(cellData -> cellData.getValue().idProperty());
        TableColumn<huodongfabu, String> name = new TableColumn<>("id");
        name.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        TableColumn<huodongfabu, String> createtime = new TableColumn<>("创建时间");
        createtime.setCellValueFactory(cellData -> cellData.getValue().createtimeProperty());
        TableColumn<huodongfabu, String> quanxianProperty = new TableColumn<>("内容");
        quanxianProperty.setCellValueFactory(cellData -> cellData.getValue().neirongProperty());


        tableView.getColumns().addAll(id, name, createtime, quanxianProperty);

        return tableView;
    }
}